package com.jinkeen.xiaxie.recycleview

import android.annotation.SuppressLint
import android.graphics.Color
import android.os.Bundle
import android.widget.LinearLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.jinkeen.xiaxie.databinding.ActivityMyRecycleViewBinding
import java.io.Serializable


class MyRecycleViewActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMyRecycleViewBinding
    var userList: ArrayList<User>? = null
    private lateinit var adapter: MyRvAdapter
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMyRecycleViewBinding.inflate(layoutInflater)
        setContentView(binding.root)
        initData()
        initListener()
    }

    @SuppressLint("WrongConstant")
    private fun initData() {
        userList = ArrayList()
        for (i in 0 until 20) {
            userList!!.add(
                User(
                    "张三$i",
                    if (i / 2 == 0) "男" else "女",
                    "1863878449$i",
                    i.toString()
                )
            )
        }
        adapter = MyRvAdapter(userList!!, this)
        val linearLayoutManager = LinearLayoutManager(this, LinearLayout.VERTICAL, false)
        binding.rv.layoutManager = linearLayoutManager
        binding.rv.adapter = adapter
        binding.top.imgBack.setOnClickListener {
            finish()
        }
    }

    private fun initListener() {
        val itemTouchHelper = ItemTouchHelper(object : ItemTouchHelper.Callback() {
            //获取touch的响应方向 1.拖动dragFlags 2.侧滑删除 swipeFlags
            override fun getMovementFlags(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder
            ): Int {
                val swipeFlags = ItemTouchHelper.LEFT

                //拖动
                val dragFlags  = if (recyclerView.layoutManager is GridLayoutManager) {
                    ItemTouchHelper.UP or ItemTouchHelper.LEFT or
                            ItemTouchHelper.DOWN or ItemTouchHelper.RIGHT
                } else {
                    ItemTouchHelper.UP or ItemTouchHelper.DOWN
                }
//                拖动暂不处理默认是0
                return makeMovementFlags(dragFlags, swipeFlags)
            }

            //拖动的时候会不断回调这个方法
            override fun onMove(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder,
                target: RecyclerView.ViewHolder
            ): Boolean {
                //起始位置
                val startPosition = viewHolder.adapterPosition
                //结束位置
                val endPosition = target.adapterPosition
                val movedUser = userList!![startPosition]
                if (startPosition > endPosition) {
                    for (i in startPosition until endPosition) {
                        userList!![endPosition] = userList!![endPosition + 1]
                    }
                } else {
                    for (i in endPosition until startPosition) {
                        userList!![startPosition] = userList!![startPosition + 1]
                    }
                }
                userList!![endPosition] = movedUser
                adapter.notifyItemMoved(startPosition, endPosition)
                return true
            }

            //            侧滑删除之后的回调
            override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
                //拖动后的位置
                val position = viewHolder.adapterPosition
                userList!!.removeAt(position)
                adapter.notifyDataSetChanged()
            }

            /**
             * 拖动状态改变回调
             */
            override fun onSelectedChanged(viewHolder: RecyclerView.ViewHolder?, actionState: Int) {
                super.onSelectedChanged(viewHolder, actionState)
                if (actionState > ItemTouchHelper.ACTION_STATE_IDLE) {
                    viewHolder!!.itemView.setBackgroundColor(Color.GRAY)
                }
            }

            /**
             * 回到正常状态回调
             */
            override fun clearView(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder
            ) {
                super.clearView(recyclerView, viewHolder)
                viewHolder.itemView.setBackgroundColor(0)
                ViewCompat.setTranslationX(viewHolder.itemView, 0f)
            }
        })

        itemTouchHelper.attachToRecyclerView(binding.rv)
    }
}

data class User(
    var name: String = "",
    var sex: String = "",
    var phone: String = "",
    var age: String = ""
) : Serializable